//
//  ToolArr.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation

extension Tools {
    
    /// JSONString转换为数组
    static public func getArrayFromJSONString(_ jsonString: String) -> NSArray? {
        guard let jsonData:Data = jsonString.data(using: .utf8) else { return nil }
        guard let array = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else { return nil }
        return array as? NSArray
    }
    
    /// JSONString转换为字典
    static public func getDictionaryFromJSONString(_ jsonString: String) ->NSDictionary{
        let jsonData: Data = jsonString.data(using: .utf8)!
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSDictionary
        }
        return NSDictionary()
    }
    
    /// 数组转换为JSONString
    static public func getJSONStringFromArray(_ array: NSArray) -> String {
        if (!JSONSerialization.isValidJSONObject(array)) { Log_y.print_("无法解析出JSONString"); return "" }
        guard let data = try? JSONSerialization.data(withJSONObject: array, options: []) as NSData else { return "" }
        guard let string = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue) else { return "" }
        return string as String
        
    }
}

extension Tools {
    
    // MARK:
    // MARK: 只包含一种元素类型的array 是否相同
    /// 只包含一种元素类型的array 是否相同
    static public func isSameArry(_ firstArray: [String],
                           _ thenArray: [String]) -> Bool {
        // 当所有元素都是 字符串 类型
        for i in firstArray {
            // 如果出现不相等 直接返回
            if thenArray.contains(i) == false {
                return false
            }
        }
        // 执行到这里说明全部相等
        return true
    }
}
